package com.xxy.book.service;

import com.xxy.book.dao.BookMapper;
import com.xxy.book.entity.Book;
import com.xxy.book.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Xu XianYun
 * @Date 2022-12-19
 * @Description
 */
@Service
public class BookService {

    @Resource
    private BookMapper bookMapper;

    /**
     * @Description 分页查询所有书籍
     * @Author Xu XiangYun
     * @Date 2022-12-19 20:08
     * @return List<Book>
     */
    public List<Book> findAllBooks(int offset, int size) {
        return bookMapper.selectAllBooks(offset, size);
    }

    /**
     * @Description 查询所有书籍数目，用于分页显示查询
     * @Author Xu XiangYun
     * @Date 2022-12-19 21:06
     * @return int
     */
    public int selectALLBooksCount() {
        return bookMapper.selectAllBooksCount();
    }

    /**
     * @Description 据书名模糊查询(分页)
     * @Author Xu XiangYun
     * @Date 2022-12-22 19:11
     * @param bookName
     * @param offset
     * @param size
     * @return List<Book>
     */
    public List<Book> findBooksByName(String bookName, int offset, int size) {
        return bookMapper.selectBookByName(bookName, offset, size);
    }

    /**
     * @Description 据作者模糊查询(分页)
     * @Author Xu XiangYun
     * @Date 2022-12-22 19:14
     * @param author
     * @param offset
     * @param size
     * @return List<Book>
     */
    public List<Book> findBooksByAuthor(String author, int offset, int size) {
        return bookMapper.selectBookByAuthor(author, offset, size);
    }

    /**
     * @Description 据书名模糊查询书籍的数目
     * @Author Xu XiangYun
     * @Date 2022-12-22 19:18
     * @param bookName
     * @return int
     */
    public int findLikeCountByName(String bookName) {
        return bookMapper.selectLikeCountByName(bookName);
    }

    /**
     * @Description 据作者模糊查询书籍的数目
     * @Author Xu XiangYun
     * @Date 2022-12-22 19:20
     * @param author
     * @return int
     */
    public int findLikeCountByAuthor(String author) {
        return bookMapper.selectLikeCountByAuthor(author);
    }

    /**
     * @Description 新增书籍
     * @Author Xu XiangYun
     * @Date 2022-12-22 19:24
     * @param book
     * @return int
     */
    public Result addBook(Book book) {
        if (bookMapper.insertBook(book) != 0) {
            return Result.ok();
        } else {
            return Result.fail("添加失败");
        }
    }

    /**
     * @Description 修改书籍信息
     * @Author Xu XiangYun
     * @Date 2022-12-22 20:02
     * @param book
     * @param id
     * @return int
     */
    public int modifyBook(int id, Book book) {
        return bookMapper.updateBookInfo(id, book);
    }
}
